package com.bcn.login_mybatis_demo.service.serviceImpl;

import com.bcn.login_mybatis_demo.mapper.UserMapper;
import com.bcn.login_mybatis_demo.mapper.UserRoleMapper;
import com.bcn.login_mybatis_demo.pojo.Role;
import com.bcn.login_mybatis_demo.pojo.User;
import com.bcn.login_mybatis_demo.pojo.UserDTO;
import com.bcn.login_mybatis_demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 大白菜
 * @date Created in 2022/9/26 14:56
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserRoleMapper userRoleMapper;

    @Override
    public User loginService(String uact, String upwd) {
        User user = userMapper.selectUserByUact(uact);
        return user;
    }


    @Override
    public String registerService(User user) {
        User userE=userMapper.selectUserByUact(user.getUaccount());
        if(userE==null){
            if("".equals(user.getUpassword())){
                return "密码为空";
            }else if("".equals(user.getUname())){
                return "用户昵称为空";
            }else{
                userMapper.insertUser(user);
                return "SUCCESS";
            }
        }else{
            return "此用户已被注册";
        }
    }

    @Override
    public List<User> getUserList(UserDTO userDTO) {
        int offset = (userDTO.getPageNo() - 1) * userDTO.getPageSize();
        int pageSize = userDTO.getPageSize();
        return userMapper.selectAllUser(userDTO, offset, pageSize);
    }
    // 获取总数据量
    @Override
    public long getUserCount(UserDTO userParams) {
        return userMapper.countUsers(userParams);
    }

    @Override
    public User queryDetail(int uid) {
        User user = userMapper.selectUserByUid(uid);
        return user;
    }

    @Override
    public String delUser(int uid) {
        User user = userMapper.selectUserByUid(uid);
        if(user!=null){
            userMapper.delUser(uid);
            return "SUCCESS";
        }
        return "用户不存在";
    }

    @Override
    public String updateUser(User user){
        if(user==null){
            return "参数不能为空";
        }else if(user.getUid()==null || user.getUid()<=0 || userMapper.selectUserByUid(user.getUid()) == null ) {
            return "请传入正确的uid";
        }else {
            userMapper.updateUser(user);
            if (user.getRoleId() != null) {
                List<Role> roleList = userRoleMapper.findRolesByUserId(user.getUid());
                if (roleList != null && !roleList.isEmpty()) {
                    // 删除旧的角色关联（如果有）
                    userRoleMapper.deleteUserRolesByUserId(user.getUid());
                }
                // 添加新的角色关联
                userRoleMapper.insertUserRole(user.getUid(), user.getRoleId());
            }
            return "SUCCESS";
        }
    }

    @Override
    public User findUserById(int uid) {
        return userMapper.selectUserByUid(uid);
    }

}
